//
//  TransferInfoViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/8.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "TransferInfoViewController.h"
#import "TransferPigInfoView.h"
#import "InputTransferInfoViewController.h"

@interface TransferInfoViewController ()
/**
 view
 */
@property (nonatomic , strong) TransferPigInfoView *transferView;
/**
 tips
 */
@property (nonatomic , strong) UILabel *tips;
/**
 btn
 */
@property (nonatomic , strong) UIButton *confirmBtn;
/**
 model
 */
@property (nonatomic , strong) TransferInfo *model;
@end

@implementation TransferInfoViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavView.navTitleLabel.text = @"仔猪转让";
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    [self.view addSubview:self.tips];
    [self.view addSubview:self.transferView];
    [self.view addSubview:self.confirmBtn];
}



- (void) initLayout {
    [self.tips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(15 + TJ_NavBarHeight);
        make.height.mas_equalTo(20);
    }];
    [self.transferView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(self.tips.mas_bottom).offset(15);
        make.height.mas_equalTo(214);
        make.right.mas_equalTo(-15);
    }];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(self.transferView.mas_bottom).offset(60);
        make.size.mas_equalTo(CGSizeMake(315, 48));
    }];
}



- (void) initAction {
    [self.confirmBtn addTarget:self action:@selector(confirmTapped) forControlEvents:UIControlEventTouchUpInside];
}


- (void)initData {
    if ([JTool iSNull:self.adoptPigId]) return;
    @weakify(self);
    [[[PigMarketApi sharedInstance] transferPigInfoWithAdoptPlanId:self.adoptPigId] subscribeNext:^(TransferInfo * _Nullable transferInfo) {
        @strongify(self);
        self.model = transferInfo;
        [self.transferView.headImg sd_setImageWithURL:[NSURL URLWithString:transferInfo.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_samll"]];
        self.transferView.pigInfoTitle.text = transferInfo.varietiesName;
        self.transferView.pigNum.text = [NSString stringWithFormat:@"编号%@",transferInfo.pigNum];
        self.transferView.birthdayDetailLab.text = transferInfo.birthDate;
        self.transferView.adoptedDateDetailLab.text = transferInfo.adoptDate;
        self.transferView.currentDaysDetailLab.text = [NSString stringWithFormat:@"%@日",transferInfo.dayAge];
        self.transferView.weightDetailLab.text = [NSString stringWithFormat:@"%@kg(%@)",transferInfo.weight,transferInfo.weightDate];
        self.transferView.dateDetailLab.text = transferInfo.expectOutDate;
    
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}


#pragma mark - delegate

#pragma mark - target action
- (void)confirmTapped {
    InputTransferInfoViewController *vc = [[InputTransferInfoViewController alloc] init];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - private method

#pragma mark - getter and setter
- (UILabel *)tips {
    if (!_tips) {
        _tips = [[UILabel alloc] init];
        _tips.textColor = [UIColor tipsGrayColor];
        _tips.font = [UIFont middleFont];
        _tips.text = @"转让接收时间最长为3天，否则将转让失败";
    }
    return _tips;
}


- (TransferPigInfoView *)transferView {
    if (!_transferView) {
        _transferView = [[TransferPigInfoView alloc] init];
        _transferView.layer.cornerRadius = 8;
        _transferView.layer.masksToBounds = YES;
        _transferView.backgroundColor = [UIColor whiteColor];
    }
    return _transferView;
}


- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"确认转让人" forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = [UIFont largerFont];
        [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"btn_nomal_bg"] forState:UIControlStateNormal];
    }
    return _confirmBtn;
}


- (TransferInfo *)model {
    if (!_model) {
        _model = [[TransferInfo alloc] init];
    }
    return _model;
}
@end
